package org.jiuling.aliyun.sms.factory.service.impl;

import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.AddShortUrlRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.DeleteShortUrlRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.QueryShortUrlRequest;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;
import org.jiuling.aliyun.sms.convert.AddShortUrlResponseConvert;
import org.jiuling.aliyun.sms.convert.DeleteShortUrlResponseConvert;
import org.jiuling.aliyun.sms.convert.QueryShortUrlResponseConvert;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.factory.AliYunSmsAsynchronous;
import org.jiuling.aliyun.sms.factory.service.AliYunShortChainManagementService;
import org.jiuling.aliyun.sms.factory.service.verify.AliYunShortChainManagementVerify;
import org.jiuling.aliyun.sms.model.AddShortUrlResponseModel;
import org.jiuling.aliyun.sms.model.DeleteShortUrlResponseModel;
import org.jiuling.aliyun.sms.model.QueryShortUrlResponseModel;

import java.util.concurrent.ExecutionException;

/**
 * 阿里异步短链管理Service实现类
 * @author 紫灵
 * @date 2023年02月21日 15时43分03秒
 */
public class AliYunAsyncShortChainManagementServiceImpl extends AliYunSmsAsynchronous implements AliYunShortChainManagementService {

    public AliYunAsyncShortChainManagementServiceImpl(AliYunSmsConfig aliYunSmsConfig) {
        super(aliYunSmsConfig);
    }

    @Override
    public AddShortUrlResponseModel addShortUrl(String sourceUrl, String shortUrlName, int effectiveDays) {
        // 校验添加短链接地址参数
        AliYunShortChainManagementVerify.addShortUrlParam(sourceUrl, shortUrlName, effectiveDays);
        // 构建添加短链接请求
        AddShortUrlRequest addShortUrlRequest = AddShortUrlRequest.builder()
                .sourceUrl(sourceUrl)
                .shortUrlName(shortUrlName)
                .effectiveDays(String.valueOf(effectiveDays))
                .build();
        // 生成客户端
        try (AsyncClient asyncClient = create()){
            return AddShortUrlResponseConvert.bodyToModel(asyncClient.addShortUrl(addShortUrlRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public DeleteShortUrlResponseModel deleteShortUrl(String sourceUrl) {
        // 校验删除短链接地址参数
        AliYunShortChainManagementVerify.deleteShortUrlParam(sourceUrl);
        // 构建删除短链接请求
        DeleteShortUrlRequest deleteShortUrlRequest = DeleteShortUrlRequest.builder()
                .sourceUrl(sourceUrl)
                .build();
        // 生成客户端
        try (AsyncClient asyncClient = create()){
            return DeleteShortUrlResponseConvert.bodyToModel(asyncClient.deleteShortUrl(deleteShortUrlRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QueryShortUrlResponseModel queryShortUrl(String shortUrl) {
        // 校验查询短链接地址参数
        AliYunShortChainManagementVerify.queryShortUrlParam(shortUrl);
        // 构建查询短链接请求
        QueryShortUrlRequest queryShortUrlRequest = QueryShortUrlRequest.builder()
                .shortUrl(shortUrl)
                .build();
        // 生成客户端
        try (AsyncClient asyncClient = create()){
            return QueryShortUrlResponseConvert.bodyToModel(asyncClient.queryShortUrl(queryShortUrlRequest).get().getBody());
        } catch (ExecutionException | InterruptedException e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }
}
